<?
// This include file holds functions needed for various language issues.
// It is meant to be designed as if it is a class - at first I wanted to make
// a class, but as it needs no instantiation and PHP does not support static
// variables, I am doing it this way and counting on programmers' discipline.

// Most important of all, DO NOT USE THE ARRAYS DIRECTLY! Their design WILL
// change! Use the functions, or write your own functions if neither of the
// existing ones is appropriate.

include_once('iso_639_list.inc');
include_once('iso_639_loc_list.inc');
include_once('lang_data.inc');

function installed_langs()
{
    if(isset($GLOBALS['inst_lang_data'])) {
        return $GLOBALS['inst_lang_data'];
    }
}

// This function returns array of all languages that are not installed on the
// site, but whose installation is planned. Languages with "strange" codes
// (hy, br, cy, ...) are those for which there is no gettext() support.
function uninstalled_langs()
{
    if(isset($GLOBALS['uninst_lang_data'])) {
        return $GLOBALS['uninst_lang_data'];
    }
}

function lang_code($shortcode)
{
    global $locales;

    $l=&$shortcode;
    //	if($locales[$l]||$locales[$l=substr($l,0,5)]||$locales[$l=substr($l,0,2)]) return $locales[$l];
    if(isset($locales[$l])
            || isset($locales[$l=substr($l,0,5)])
            || isset($locales[$l=substr($l,0,2)]))
        return $locales[$l];
    else
        return false;
}

function short_lang_code($langcode = false)
{
    return "en";
}

// Returns ISO 639 code(s) for language(s) of a project.
// $which could be "primary", "secondary" or "all":
// - "primary" returns the code of primary language
// - "secondary" returns the code of secondary language, or NULL if the
//   secondary language doesn't exist
// - "all" returns array with the code(s) of both language(s); if the secondary
//   language doesn't exist, the array only has one element
// If a language doesn't have ISO 639 code, NULL is returned; this might change
// Some code copied from tools/proofers/spellcheck_text.inc
function proj_lang_code($proj_lang, $which="all")
{
    global $iso_639;

    $languages = preg_split('/ with /', $proj_lang );

    if($which == 'primary')
        return array_search( $languages[0], $iso_639 );
    elseif($which == 'secondary')
        return array_search( $languages[1], $iso_639 );
    elseif($which == 'all') {
        $res = array();
        foreach($languages as $v)
            $res[] = array_push( $res, array_search( $v, $iso_639 ));
        return $res;
    }
}

function lang_name($langcode)
{
    global $iso_639_loc,$lang_name_data;

    if(isset($lang_name_data[$langcode]))
        return $lang_name_data[$langcode];
    else
        return $iso_639_loc[substr($langcode,0,2)];
}

function eng_name($langcode)
{
    global $iso_639,$eng_name_data;

    if(isset($eng_name_data[$langcode]))
        return $eng_name_data[$langcode];
    else
        return $iso_639[substr($langcode,0,2)];
}

function bilingual_name($langcode)
{
    $a = eng_name($langcode);
    $b = lang_name($langcode);

    if($a == $b)
        return $a;
    elseif(!$a)
        return $b;
    else
        return trim("$b ($a)");
}

function lang_direction($langcode = false)
{
    return "en";
    //	if($langcode === FALSE)
    //	   $langcode = $GLOBALS['locale'];
    //
    //	switch(substr($langcode, 0, 2)) {
    //	        case "ar":
    //	        case "fa":
    //	        case "he":
    //	        case "ur": //surely more
    //	                return FALSE;
    //	        default:
    //	                return TRUE;
    //	}
}

function lang_dir($langcode = false)
{
    global $lang_dir_data;

    if(! $langcode)
        $langcode = @$GLOBALS['locale'];
    if($langcode) {
        $a = substr($langcode, 0, 2);
        if(in_array($a, $lang_dir_data))
            return $a."/";
    }
    return "";
}

// This function returns forum of a particular language. If there is no such
// forum, it might return a team thread or whatever is appropriate (or nothing,
// of course).
function lang_forum($langcode = false)
{
    global $lang_forum_data;

    if(empty($langcode))
        $langcode = @$GLOBALS['locale'];
    if(! empty($langcode))
        return @$lang_forum_data[$langcode];
    else
        return false;
}

function phpbb_lang($langcode = false)
{
    global $phpbblangs;

    if( !$langcode)
        $langcode = $GLOBALS['locale'];
    return $phpbblangs[$langcode];
}

function lang_html_header($langcode = false)
{
    return " lang='" 
        .short_lang_code($langcode) 
        .lang_direction($langcode) 
        ? "'" 
        : "' dir='RTL'";
}

?>
